﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HB.WorkFlow;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {

            var wf = new HB.WorkFlow.WorkFlow<Data>("pdf", new filter());
            wf.BuildStep<FetchTask>().GoStep<LoadTask>();
            wf.BuildStep<LoadTask>().GoStep<DoJs>();
            wf.BuildStep<DoJs>().GoStep<Move>();
            wf.BuildStep<Move>().GoStepIf<Clear>(t => false, "发送错误").GoStepElse<SavePdf>();
            wf.BuildStep<SavePdf>().GoStep<Clear>();
            wf.BuildStep<Clear>().GoStepIf<FetchTask>(d => d.ExecCount < 6, "ExecCount < 6");

            wf.Start<FetchTask>(new Data(), "1");
            wf.ShowFlowChartView();



            Console.ReadLine();
        }
    }
    class filter : IStepFilter
    {
        public void OnErr(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }



        public void RouteNavigated<T>(StepBody<T> step, T data, string targetStep) where T : class
        {
            //Console.WriteLine("导航到："+targetStep);
        }

        public void StepExecuted<T>(StepBody<T> step, T data) where T : class
        {
            //  Console.WriteLine("执行完：" + step.Step.ShowName );

        }

        public void StepExecuting<T>(StepBody<T> step, T data) where T : class
        {
            // Console.WriteLine("执行前：" + step.Step.ShowName);
        }



        public void WorkFlowEnd<T>(WorkFlow<T> wf, StepBody<T> step, T data, string sampleId) where T : class
        {
            //Console.WriteLine($"当前'{step.Step.Name}'工作流结束");
            //var ls = wf.GetSampleExecLog(sampleId);
            //foreach (var log in ls)
            //{
            //    Console.WriteLine(log);
            //}
        }

        public void WorkFlowStart<T>(StepBody<T> step, T data) where T : class
        {
            Console.WriteLine($"工作流开始:{data}");

        }

        public void WorkFlowStart<T>(WorkFlow<T> wf, StepBody<T> step, T data, string sampleId) where T : class
        {
            Console.WriteLine($"工作流开始:{data}");
        }
    }
    class Data
    {
        public int stepnum = 0;
        public int id { get; set; }
        public int ExecCount { get; set; }
    }
    class LoadTask : IStep<Data>
    {
        public string Name => "加载任务";

        public Task Do(Data data)
        {
            return Task.Run(() =>
            {
                Thread.Sleep(1000);
                data.stepnum = 2;
                Console.WriteLine(Name);
            });


        }
    }
    class FetchTask : IStep<Data>
    {
        public string Name => "获取任务";

        public Task Do(Data data)
        {
            return Task.Run(() =>
            {
                Thread.Sleep(1000); data.stepnum = 1;
                Console.WriteLine(Name);
            });


        }
    }
    class DoJs : IStep<Data>
    {
        public string Name => "执行脚本";


        public Task Do(Data data)
        {
            return Task.Run(() =>
            {
                Thread.Sleep(1000); data.stepnum = 3;
                Console.WriteLine(Name);
            });


        }
    }
    class Move : IStep<Data>
    {
        public string Name => "滚动屏幕";

        public Task Do(Data data)
        {
            return Task.Run(() =>
            {
                Thread.Sleep(1000); data.stepnum = 4;
                Console.WriteLine(Name);
            });


        }
    }
    class SavePdf : IStep<Data>
    {
        public string Name => "保存pdf";


        public Task Do(Data data)
        {
            return Task.Run(() =>
            {
                Thread.Sleep(1000); data.stepnum = 5;
                Console.WriteLine(Name);
            });


        }
    }
    class Clear : IStep<Data>
    {
        public string Name => "清理";

        public Task Do(Data data)
        {
            return Task.Run(() =>
            {
                Thread.Sleep(1000); data.stepnum = 6;
                data.ExecCount++;
                Console.WriteLine($"exec {data.ExecCount}");
                Console.WriteLine(Name);
            });


        }
    }
}
